#ifndef _TSERVER_H_
#define _TSERVER_H_
#include <map>
#include <vector>
#include "tbind.h"
#include "tlogger.h"
#include "tsocket.h"
#include "tbuffer.h"
#include "tclient.h"
#include "ttaskqueue.h"
#include "teventloop.h"
#include "tthreadpool.h"
#include "tnocopyable.h"
namespace tlib
{
#define IO_THREAD_COUNT 4
#define BS_THREAD_COUNT 32

#ifdef WIN32
#define IO_IDX(FD) FD/4%IO_THREAD_COUNT
#define BS_IDX(FD) FD/4%BS_THREAD_COUNT
#else
#define IO_IDX(FD) FD%IO_THREAD_COUNT
#define BS_IDX(FD) FD%BS_THREAD_COUNT
#endif

class TServer: TNoCopyable
{
	friend class TEventLoop;
public:
	TServer();
	virtual ~TServer();

	bool Listen(char* host, int port);
	void Start();
	void Stop();

	virtual void OnMain() = 0;
	virtual void OnConn(TClient* conn) = 0;
	virtual void OnRecv(TClient* conn, STMSG* msg) = 0;
	virtual void OnClose(TClient* conn) = 0;
	virtual bool PreRecv(TClient* conn, STMSG* msg) = 0;
protected:
	int m_nSrvFd;
	TThread m_ioThreads[IO_THREAD_COUNT];
	TThread m_bsThreads[BS_THREAD_COUNT];
	TTaskQueue m_bsQueues[BS_THREAD_COUNT];
	TEventLoop m_eventLoop[IO_THREAD_COUNT];

	void RunInBSThread(int i);
};
}//namespace tlib

#endif
